package com.cskaoyan.service.impl;

import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.service.WxGoodsService;
import com.cskaoyan.util.Constant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author rebaogu
 * @description wx端 商品实现类
 * @date 2022.04.08 22:00
 * @package com.cskaoyan.service.impl
 * @Classname WxGoodsServiceImpl
 */
@Service
public class WxGoodsServiceImpl implements WxGoodsService {

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketGrouponMapper marketGrouponMapper;

    @Autowired
    MarketAdMapper marketAdMapper;

    // 品牌商
    @Autowired
    MarketBrandMapper marketBrandMapper;

    //
    @Autowired
    MarketTopicMapper marketTopicMapper;

    // 规格
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;


    // issue
    @Autowired
    MarketIssueMapper marketIssueMapper;

    //  collect
    @Autowired
    MarketCollectMapper marketCollectMapper;

    // 评论
    @Autowired
    MarketCommentMapper marketCommentMapper;

    // 用户
    @Autowired
    MarketUserMapper marketUserMapper;

    // 商品参数表
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;

    // 商品货品表
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    // 商品搜索历史表
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Autowired
    CouponService couponService;

    // 商品足迹表
    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public HomeIndexVO homeIndex() {

        Integer market_wx_index_hot = Integer.valueOf(marketSystemMapper.selectByKeyname("market_wx_index_hot"));
        Integer market_wx_index_brand = Integer.valueOf(marketSystemMapper.selectByKeyname("market_wx_index_brand"));
        Integer market_wx_index_new = Integer.valueOf(marketSystemMapper.selectByKeyname("market_wx_index_new"));
        Integer market_wx_catlog_list = Integer.valueOf(marketSystemMapper.selectByKeyname("market_wx_catlog_list"));
        Integer market_wx_catlog_goods = Integer.valueOf(marketSystemMapper.selectByKeyname("market_wx_catlog_goods"));

        HomeIndexVO homeIndexVO = new HomeIndexVO();

        // 1,获取商品表信息,并封装进去
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        goodsExample.setDistinct(true);
        MarketGoodsExample.Criteria criteria2Goods = goodsExample.createCriteria();
        criteria2Goods.andDeletedEqualTo(false).andCounterPriceGreaterThan(BigDecimal.valueOf(500.0));
        goodsExample.setOrderByClause("add_time desc limit " + market_wx_index_new);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(goodsExample);
        homeIndexVO.setNewGoodsList(marketGoods);

        // 2,优惠券信息表
        // 更新过期的优惠券状态
        couponService.updateCouponStatus();
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria2Coupon = marketCouponExample.createCriteria();
        criteria2Coupon.andDeletedEqualTo(false).andStatusEqualTo((short) 0).andTypeEqualTo((short) 0);
        marketCouponExample.setOrderByClause("add_time desc limit 3");

        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        /*List<MarketCoupon> marketCoupons1 = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            marketCoupons1.add(marketCoupons.get(i));
        }*/

        homeIndexVO.setCouponList(marketCoupons);

        // 3, 类目表
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria2Category = example.createCriteria();
        criteria2Category.andLevelEqualTo("L1").andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc limit " + market_wx_catlog_list);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        homeIndexVO.setChannel(marketCategories);

        // 4, 团购活动表
        MarketGrouponExample marketGrouponExample = new MarketGrouponExample();
        MarketGrouponExample.Criteria criteria2Group = marketGrouponExample.createCriteria();
        criteria2Group.andDeletedEqualTo(false);

        List<MarketGroupon> marketGroupons = marketGrouponMapper.selectByExample(null);
        // homeIndexVO.setCouponList(marketCoupons);
        homeIndexVO.setGrouponList(null);

        // 5, 广告表
        List<MarketAd> marketAds = marketAdMapper.selectByExample(null);
        homeIndexVO.setBanner(marketAds);

        // 6, 品牌商表
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria2Brand = marketBrandExample.createCriteria();
        marketBrandExample.setOrderByClause("add_time desc limit " + market_wx_index_brand);
        criteria2Brand.andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        homeIndexVO.setBrandList(marketBrands);

        // 7,属性为ishot ==true的商品表
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        marketGoodsExample.setOrderByClause("add_time desc limit " + market_wx_index_hot);

        criteria.andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<MarketGoods> marketGoodsIsHot = marketGoodsMapper.selectByExample(marketGoodsExample);
        homeIndexVO.setHotGoodsList(marketGoodsIsHot);

        // 8, 主题表
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(null);
        homeIndexVO.setTopicList(marketTopics);


        // 9, 类目下所有商品
        List<HomeIndexVO.FloorGoodsListDTO> floorGoodsListDTOS = new ArrayList<>();
        // 获取所有目录 id与name
        for (MarketCategory marketCategory : marketCategories) {
            HomeIndexVO.FloorGoodsListDTO floorGoodsListDTO = new HomeIndexVO.FloorGoodsListDTO();

            Integer id = marketCategory.getId();
            String name = marketCategory.getName();

            MarketGoodsExample marketGoodsExample2SelectGoods4Category = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria4Category = marketGoodsExample2SelectGoods4Category.createCriteria();
            marketGoodsExample2SelectGoods4Category.setOrderByClause("add_time desc limit " + market_wx_index_new);
            criteria4Category.andCategoryIdEqualTo(id);

            List<MarketGoods> marketGoods4Category
                    = marketGoodsMapper.selectByExample(marketGoodsExample2SelectGoods4Category);
            floorGoodsListDTO.setId(id);
            floorGoodsListDTO.setName(name);
            floorGoodsListDTO.setGoodsList(marketGoods4Category);

            floorGoodsListDTOS.add(floorGoodsListDTO);
        }
        homeIndexVO.setFloorGoodsList(floorGoodsListDTOS);

        return homeIndexVO;
    }

    @Override
    public int goodsCount() {

        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        goodsExampleCriteria.andDeletedEqualTo(false);
        Integer count = Math.toIntExact(marketGoodsMapper.countByExample(goodsExample));
        return count;
    }


    @Override
    public GoodsCategoryVO goodsCategory(Integer id) {
        // 1,获取当前分类的信息
        // 查询id类目的信息
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andIdEqualTo(id);

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);

        MarketCategory category2Current = new MarketCategory();
        ArrayList<MarketCategory> categoriesBrotherList = new ArrayList<>();
        MarketCategory category2Father = new MarketCategory();
        // 查询当前目录信息 --> 一个id 对应一个
        for (MarketCategory marketCategory : marketCategories) {
            String level = marketCategory.getLevel();

            // 如果是父分类 ,则将第一个加载的类目作为current
            if ("L1".equals(level)) {
                // Integer idL1 = marketCategory.getPid();
                // 根据pid 查找二级目录
                MarketCategoryExample example2L24Pid = new MarketCategoryExample();
                MarketCategoryExample.Criteria criteria2L24Pid = example2L24Pid.createCriteria();
                criteria2L24Pid.andDeletedEqualTo(false).andPidEqualTo(id);

                List<MarketCategory> marketCategories2L24Pid = marketCategoryMapper.selectByExample(example2L24Pid);
                // 第一个为current
                int i = 0;
                for (MarketCategory category : marketCategories2L24Pid) {
                    i++;
                    // brother
                    categoriesBrotherList.add(category);
                    // current
                    if (i == 1) {
                        category2Current = category;
                    }
                }
                // father
                category2Father = marketCategory;
            }
            // 如果为子目录
            if ("L2".equals(level)) {
                Integer pid = marketCategory.getPid();

                MarketCategoryExample example2L2 = new MarketCategoryExample();
                MarketCategoryExample.Criteria criteria2L2 = example2L2.createCriteria();
                criteria2L2.andDeletedEqualTo(false).andPidEqualTo(pid);
                // 二级目录
                List<MarketCategory> marketCategories2L2 = marketCategoryMapper.selectByExample(example2L2);
                //  brother
                for (MarketCategory category : marketCategories2L2) {
                    categoriesBrotherList.add(category);
                }
                // current
                criteria2L2.andIdEqualTo(id);
                List<MarketCategory> marketCategories2Current = marketCategoryMapper.selectByExample(example2L2);
                for (MarketCategory category : marketCategories2Current) {
                    category2Current = category;
                }
                // father
                // deleted + id + pid
                criteria2L2.andPidEqualTo(pid);
                List<MarketCategory> marketCategories2L24Pid = marketCategoryMapper.selectByExample(example2L2);
                for (MarketCategory category : marketCategories2L24Pid) {
                    category2Father = category;
                }

            }

        }
        return new GoodsCategoryVO(category2Current, categoriesBrotherList, category2Father);
    }

    @Override
    public WxGoodsListVO goodsList(BaseParam param, Integer brandId, Integer categoryId, String keyword, Integer userId, boolean isHot, boolean isNew) {
        // 获取配置项信息
        Integer market_wx_index_new = Integer.valueOf(marketSystemMapper.selectByKeyname("market_wx_index_new"));

        Integer market_wx_index_hot = Integer.valueOf(marketSystemMapper.selectByKeyname("market_wx_index_hot"));


        // 共三种情况

        // 第一种,通过商品目录id查找 商品 只会通过二级目录id来调用
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        // 需要进行判空
        String order = param.getOrder();
        String sort = param.getSort();

        // 开启分页
        PageHelper.startPage(page, limit);

        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();




        // 返回的json数据格式
        // 1, 通过目录id  返回的目录信息
        List<MarketGoods> marketGoods = new ArrayList<>();
        List<MarketCategory> marketCategories4Pid = new ArrayList<>();

        // 通过目录 id + keyword 返回信息
        List<MarketGoods> marketGoods4Keyword = new ArrayList<>();
        List<MarketCategory> marketCategories4Pid4Keyword = new ArrayList<>();

        // 如果categoryId不为空 ,才会执行
        if (categoryId != null) {
            criteria.andDeletedEqualTo(false).andCategoryIdEqualTo(categoryId);
            // 返回的商品列表
            marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);

            // 找到通过id找到当前目录的pid 再从pid找到brother目录

            MarketCategoryExample categoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria2Category = categoryExample.createCriteria();
            criteria2Category.andDeletedEqualTo(false).andIdEqualTo(categoryId);

            List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(categoryExample);
            Integer pid = -1;
            for (MarketCategory marketCategory : marketCategories) {
                pid = marketCategory.getPid();
            }

            MarketCategoryExample categoryExample4Pid = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria2Category4Pid = categoryExample4Pid.createCriteria();
            criteria2Category4Pid.andDeletedEqualTo(false).andPidEqualTo(pid);

            // 查询出来当前目录下的所有brother目录,返回回去
            marketCategories4Pid = marketCategoryMapper.selectByExample(categoryExample4Pid);

            // 第二种, 通过关键字 + 传入的排序依据 + 正序倒序
            // todo 如果有时间的话 可以使用 每一个字来进行 搜索 匹配的字越多,显示的越靠前
            // 1, 加上关键字 信息搜索 + 2, 实现 添加搜索历史到 market_search_history 表 3,实现排序
            // if (keyword != null && categoryId == 0) {
            if (categoryId == 0) {
                // 通过keyword 找到 --> 商品id 数组 --> 遍历数组 找到分类
                MarketGoodsExample marketGoodsExample4Keyword = new MarketGoodsExample();
                MarketGoodsExample.Criteria keywordCriteria = marketGoodsExample4Keyword.createCriteria();
                if (keyword != null) {
                criteria2Category.andKeywordsEqualTo(keyword);
                // 排序
                if (order != null && !"".equals(order) && sort != null && !"".equals(sort)) {
                    marketGoodsExample4Keyword.setOrderByClause(sort + " " + order);
                }
                // 模糊查询
                keywordCriteria.andDeletedEqualTo(false).andNameLike("%" + keyword + "%");
                }

                if (isHot) {
                    if (order != null && !"".equals(order) && sort != null && !"".equals(sort)) {
                        marketGoodsExample4Keyword.setOrderByClause(sort + " " + order + " limit " + market_wx_index_hot);
                    }
                    keywordCriteria.andIsHotEqualTo(true);
                }
                if (isNew) {
                    if (order != null && !"".equals(order) && sort != null && !"".equals(sort)) {
                        marketGoodsExample4Keyword.setOrderByClause(sort + " " + order + " limit " + market_wx_index_new);
                    }
                    keywordCriteria.andIsNewEqualTo(true);
                }
                List<MarketGoods> goodsList4Keyword = marketGoodsMapper.selectByExample(marketGoodsExample4Keyword);

                // 多个商品 --> 遍历得到商品分类id --> 将商品分类信息 插入list 返回给前台
                // List<Integer> categoryIdS = new ArrayList<>();
                List<MarketCategory> marketCategories1 = null;
                for (MarketGoods goods : goodsList4Keyword) {
                    Integer goodsCategoryId = goods.getCategoryId();
                    // categoryIdS.add(goodsCategoryId);
                    MarketCategoryExample categoryExample1 = new MarketCategoryExample();
                    MarketCategoryExample.Criteria categoryCriteria = categoryExample1.createCriteria();

                    categoryCriteria.andDeletedEqualTo(false).andIdEqualTo(goodsCategoryId);

                    // 去重
                    categoryExample1.setDistinct(true);

                    marketCategories1 = marketCategoryMapper.selectByExample(categoryExample1);
                }

                //     赋值
                marketGoods4Keyword = goodsList4Keyword;
                marketCategories4Pid4Keyword = marketCategories1;

                // 2, 添加搜索历史
                // 如果没有登录 搜索不会添加到表

                if (keyword != null) {
                    MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
                    MarketSearchHistoryExample.Criteria historyExampleCriteria = marketSearchHistoryExample.createCriteria();
                    historyExampleCriteria.andDeletedEqualTo(false).andKeywordEqualTo(keyword);
                    Integer count = Math.toIntExact(marketSearchHistoryMapper.countByExample(marketSearchHistoryExample));
                    if (userId != null && !"".equals(userId) && userId != -1&& count == 0) {// 登录
                        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
                        marketSearchHistory.setId(null);
                        marketSearchHistory.setUserId(userId);
                        marketSearchHistory.setKeyword(keyword);
                        marketSearchHistory.setFrom("wx");
                        marketSearchHistory.setAddTime(new Date());
                        marketSearchHistory.setUpdateTime(null);
                        marketSearchHistory.setDeleted(false);
                        marketSearchHistoryMapper.insert(marketSearchHistory);
                    }
                }
                // if (userId != null && !"".equals(userId) && userId != -1 && count == 1) {// 登录
            }
        }

        // 第三种 ,通过品牌商id查找 商品
        //     通过brandid 响应的数据
        // 商品信息
        List<MarketGoods> marketGoods4BrandId = new ArrayList<>();
        // 目录信息
        List<MarketCategory> marketCategories4BrandId = new ArrayList<>();
        if (brandId != null) {
            //  2.1 加上品牌商id筛选条件
            MarketGoodsExample marketGoodsExample4BrandId = new MarketGoodsExample();
            MarketGoodsExample.Criteria goodsExample4BrandIdCriteria = marketGoodsExample4BrandId.createCriteria();
            goodsExample4BrandIdCriteria.andDeletedEqualTo(false).andBrandIdEqualTo(brandId);

            marketGoods4BrandId = marketGoodsMapper.selectByExample(marketGoodsExample4BrandId);

            // 2.2 通过品牌商id 在goods表中找到商品目录id
            // 通过
            List<Integer> categoryIdS = marketGoodsMapper.selectByBrandId2CategoryId(brandId);

            for (Integer categoryId4BrandId : categoryIdS) {
                MarketCategoryExample categoryExample = new MarketCategoryExample();
                MarketCategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
                categoryExampleCriteria.andDeletedEqualTo(false).andIdEqualTo(categoryId4BrandId);

                List<MarketCategory> marketCategoryList = marketCategoryMapper.selectByExample(categoryExample);
                marketCategories4BrandId = marketCategoryList;
            }

        }


        // 分页
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoods);
        Integer total = Math.toIntExact(pageInfo.getTotal());
        int pages = pageInfo.getPages();

        if (brandId != null && !"".equals(brandId)) {
            return new WxGoodsListVO(total, pages, limit, page, marketGoods4BrandId, marketCategories4BrandId);
        } else if (categoryId != null && !"".equals(categoryId)) {// categoryID
            if (categoryId != 0) {
                return new WxGoodsListVO(total, pages, limit, page, marketGoods, marketCategories4Pid);
            }
        }
        return new WxGoodsListVO(total, pages, limit, page, marketGoods4Keyword, marketCategories4Pid4Keyword);
    }

    @Override
    public WxGoodsDetailVO goodsDetail(Integer id, MarketUser user) {// 传进来的是goodsId
        // 第一 需要实现 用户访问详情页面时,将用户访问信息存储到用户足迹表
        if (user != null && !"".equals(user)) {
            Integer userId = user.getId();
            if (userId != null && !"".equals(userId)) {
                MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
                MarketFootprintExample.Criteria footprintExampleCriteria = marketFootprintExample.createCriteria();
                footprintExampleCriteria.andDeletedEqualTo(false);

                MarketFootprint marketFootprint = new MarketFootprint(
                        null
                        , userId
                        , id
                        , new Date()
                        , null
                        , false
                );
                marketFootprintMapper.insert(marketFootprint);
            }
        }

        // Integer userId = 1;

        // 对user判空

        // 每查询到一个数值,就直接在当下set进去
        WxGoodsDetailVO wxGoodsDetailVO = new WxGoodsDetailVO();

        // 1, 获取规格信息
        // 需要返回的信息
        ArrayList<WxGoodsDetailVO.SpecificationListDTO> specificationListDTOS = new ArrayList<>();

        MarketGoodsSpecificationExample goodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria2Specific = goodsSpecificationExample.createCriteria();
        criteria2Specific.andDeletedEqualTo(false).andGoodsIdEqualTo(id);

        List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByExample(goodsSpecificationExample);

        for (MarketGoodsSpecification marketGoodsSpecification : marketGoodsSpecifications) {
            WxGoodsDetailVO.SpecificationListDTO specificationListDTO = new WxGoodsDetailVO.SpecificationListDTO();
            // 1, 查出name
            String name = marketGoodsSpecification.getSpecification();
            // 2, 查出当前name下的规格信息list
            criteria2Specific.andSpecificationEqualTo(name);
            List<MarketGoodsSpecification> marketGoodsSpecifications2Name = marketGoodsSpecificationMapper.selectByExample(goodsSpecificationExample);
            // 3, 封装
            specificationListDTO.setName(name);
            specificationListDTO.setValueList(marketGoodsSpecifications2Name);
            specificationListDTOS.add(specificationListDTO);
        }
        wxGoodsDetailVO.setSpecificationList(specificationListDTOS);
        // 2,
        // todo  后面做 --> 团购信息
        // wxGoodsDetailVO.setGroupon(new String[]{"团购一下", "有你也有我"});
        wxGoodsDetailVO.setGroupon(null);


        // 3, issue信息 deted
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria issueExampleCriteria = marketIssueExample.createCriteria();
        issueExampleCriteria.andDeletedEqualTo(false);// todo 判断条件不清楚
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(marketIssueExample);
        wxGoodsDetailVO.setIssue(marketIssues);

        // 4, 通过 useid 来判断是否收藏 --> market_collect -->
        if (user == null) {
            wxGoodsDetailVO.setUserHasCollect(0);
        } else {
            Integer userId = user.getId();
            MarketCollectExample marketCollectExample = new MarketCollectExample();
            MarketCollectExample.Criteria collectExampleCriteria = marketCollectExample.createCriteria();
            collectExampleCriteria.andDeletedEqualTo(false).andUserIdEqualTo(userId);

            List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);

            int collect = 0;
            if (!marketCollects.isEmpty()) {
                for (MarketCollect marketCollect : marketCollects) {
                    if (marketCollect.getType() == 0 && marketCollect.getValueId().equals(id)) {
                        collect = 1;
                    }
                }
            }
            wxGoodsDetailVO.setUserHasCollect(collect);
        }

        // 5, 图片
        // todo 图片添加
        MarketGoodsExample goodsExample2ShareImage = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria2ShareImage = goodsExample2ShareImage.createCriteria();
        criteria2ShareImage.andDeletedEqualTo(false).andIdEqualTo(id);
        List<MarketGoods> marketGoods2ShareImage = marketGoodsMapper.selectByExample(goodsExample2ShareImage);

        String S = "";
        for (MarketGoods marketGood : marketGoods2ShareImage) {
            String[] gallery = marketGood.getGallery();
            for (int i = 0; i < gallery.length; i++) {
                S = S + gallery[i] +",";
            }
        }
        S.replace("[", "");
        S.replace("]", "");
        S.replace("null", "");
        String[] split = S.split(",");
        wxGoodsDetailVO.setShareImage(split);

        // 6, 评论表 通过userId
        WxGoodsDetailVO.CommentDTO commentDTO = new WxGoodsDetailVO.CommentDTO();

        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria commentExampleCriteria = commentExample.createCriteria();
        // 通过 goodsid -->就是 comment中的 Valueid字段查询信息
        // --> 再通过 comment中的表的信息,查询到userid
        // 通过userid查询到--> nickname

        commentExampleCriteria.andDeletedEqualTo(false).andValueIdEqualTo(id);
        commentExample.setOrderByClause("add_time desc limit 3");
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(commentExample);
        // 需要返回的值
        List<WxGoodsDetailVO.CommentDTO.DataDTO> dataDTOS = new ArrayList<>();
        for (MarketComment marketComment : marketComments) {
            WxGoodsDetailVO.CommentDTO.DataDTO dataDTO = new WxGoodsDetailVO.CommentDTO.DataDTO();
            dataDTO.setAvatar(marketComment.getAdminContent());
            Integer userId2NicknameAndAvatar = marketComment.getUserId();

            MarketUserExample userExample = new MarketUserExample();
            MarketUserExample.Criteria userExampleCriteria = userExample.createCriteria();
            userExampleCriteria.andDeletedEqualTo(false).andIdEqualTo(userId2NicknameAndAvatar);

            List<MarketUser> marketUsers = marketUserMapper.selectByExample(userExample);

            // 查询到的user只有一个
            for (MarketUser marketUser : marketUsers) {
                String nickname = marketUser.getNickname();
                String avatar = marketUser.getAvatar();

                dataDTO.setNickname(nickname);

            }
            dataDTO.setAddTime(marketComment.getAddTime());
            dataDTO.setPicList(marketComment.getPicUrls());
            dataDTO.setAdminContent(marketComment.getAdminContent());
            dataDTO.setId(marketComment.getId());
            dataDTO.setContent(marketComment.getContent());
            dataDTOS.add(dataDTO);
        }
        commentDTO.setData(dataDTOS);
        // 查询评论总数
        Integer count = Math.toIntExact(marketCommentMapper.countByExample(commentExample));
        commentDTO.setCount(count);

        wxGoodsDetailVO.setComment(commentDTO);

        // 7, 是否可以分享 --> 直接写死 --> 那个商品不能分享
        wxGoodsDetailVO.setShare(true);

        // 8, goodsid --> market_goods_attribute --> 商品参数表
        MarketGoodsAttributeExample goodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria attributeExampleCriteria = goodsAttributeExample.createCriteria();
        attributeExampleCriteria.andDeletedEqualTo(false).andGoodsIdEqualTo(id);

        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByExample(goodsAttributeExample);

        wxGoodsDetailVO.setAttribute(marketGoodsAttributes);

        // 9, goodsid --> 所属的品牌商的信息
        // goodsId --> brandId --> brand表信息

        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria2Goods = goodsExample.createCriteria();
        criteria2Goods.andDeletedEqualTo(false).andIdEqualTo(id);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(goodsExample);

        Integer brandId = -1;
        for (MarketGoods marketGood : marketGoods) {
            brandId = marketGood.getBrandId();
        }

        MarketBrandExample brandExample = new MarketBrandExample();
        MarketBrandExample.Criteria brandExampleCriteria = brandExample.createCriteria();
        brandExampleCriteria.andDeletedEqualTo(false).andIdEqualTo(brandId);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(brandExample);
        // 其实只有一个
        for (MarketBrand marketBrand : marketBrands) {
            wxGoodsDetailVO.setBrand(marketBrand);
        }

        // 10, goodsid --> market_goods_product--> 商品货品表
        MarketGoodsProductExample goodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria productExampleCriteria = goodsProductExample.createCriteria();
        productExampleCriteria.andDeletedEqualTo(false).andGoodsIdEqualTo(id);

        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(goodsProductExample);

        wxGoodsDetailVO.setProductList(marketGoodsProducts);

        // 11, goodsid --> 商品表
        MarketGoods marketGoods1 = marketGoodsMapper.selectByPrimaryKey(id);
        wxGoodsDetailVO.setInfo(marketGoods1);

        return wxGoodsDetailVO;
    }

    @Override
    public GoodsRelatedVO goodsRelated(Integer goodsId) {
        int goodsRelatedLimit = Constant.goodsRelatedLimit;// 一页有几个
        int goodsRelatedPage = Constant.goodsRelatedPage;// 当前页

        // 开启分页
        PageHelper.startPage(goodsRelatedPage, goodsRelatedLimit);

        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria2Goods = goodsExample.createCriteria();
        criteria2Goods.andDeletedEqualTo(false).andIdEqualTo(goodsId);

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(goodsExample);
        // 获取当前商品的分类id
        Integer categoryId = 0;
        for (MarketGoods marketGood : marketGoods) {
            categoryId = marketGood.getCategoryId();
        }
        // 查询当前分类的商品
        MarketGoodsExample goodsExample2Category = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria2Goods2Category = goodsExample2Category.createCriteria();
        criteria2Goods2Category.andDeletedEqualTo(false)
                .andCategoryIdEqualTo(categoryId)
                .andIdNotEqualTo(goodsId);
        List<MarketGoods> marketGoods2Category = marketGoodsMapper.selectByExample(goodsExample2Category);


        List<MarketGoods> marketGoods1 = new ArrayList<>();
        int i = 0;
        for (MarketGoods goods : marketGoods2Category) {
            marketGoods1.add(goods);
            i++;
            if (i == 6) {
                break;
            }
        }

        // 分页
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoods2Category);
        Integer total = Math.toIntExact(pageInfo.getTotal());
        int pages = pageInfo.getPages();

        return new GoodsRelatedVO(6, pages, goodsRelatedLimit, goodsRelatedPage, marketGoods1);
    }


    // public <T> void  AddDeleted(T t) {
    //     T example = new T();
    //     T.Criteria criteria = example.createCriteria();
    //     criteria.andDeletedEqualTo(false);
    // }
}
